package com.adams.app.ui;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.adams.app.R;
import com.adams.app.common.Global;
import com.adams.app.data.RequestManager;
import com.adams.app.model.Attractions;
import com.adams.app.vendor.HappyGyApi;

public class TravelDetailActivity extends BaseActivity {

	private ImageView travel_detail_logo;
	private TextView travel_detail_ticket_price, tv_travel_detail_address,
			tv_travel_detail_phone, travel_detail_play_time,
			travel_detail_open_time, tv_scenic_detail_assess,
			tv_scenic_detail_jjcontent;
	private Attractions attractions;
	private RelativeLayout travel_detail_address, travel_detail_phone;
	private LinearLayout travel_detail_ll;
	private Drawable mDefaultImageDrawable = new ColorDrawable(Color.argb(255,
			201, 201, 201));

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_travel_detail);

		attractions = (Attractions) getIntent().getExtras().getSerializable(
				"attractions");
		
		this.findView();
		this.setData();
		this.setListener();
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	public void setListener() {
		travel_detail_logo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(TravelDetailActivity.this,
						ImageDetailsActivity.class).putExtra("id",
						attractions.getId()));
			}
		});

		travel_detail_phone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String phoneNumber = attractions.getTel().trim();
				Intent intent = new Intent(Intent.ACTION_DIAL, Uri
						.parse("tel://" + phoneNumber));
				startActivity(intent);
			}
		});

		travel_detail_address.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(TravelDetailActivity.this, "正在定位...",
						Toast.LENGTH_SHORT).show();
				String address = tv_travel_detail_address.getText().toString();
				Intent intent = new Intent(TravelDetailActivity.this,
						MyMapActivity.class);
				intent.putExtra(Global.ADDRESS, address);
				startActivity(intent);
			}
		});

		travel_detail_ll.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(TravelDetailActivity.this,
						ScrollViewActivity.class);
				intent.putExtra("title", attractions.getName());
				intent.putExtra("url", attractions.getUrlPic());
				intent.putExtra("context", attractions.getIntro());
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		menu.add("图集")
				.setTitle("图集")
				.setIntent(
						new Intent(TravelDetailActivity.this,
								ImageDetailsActivity.class).putExtra("id",
								attractions.getId()))
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		return super.onCreateOptionsMenu(menu);
	}

	// 实例化控件
	public void findView() {
		travel_detail_logo = (ImageView) findViewById(R.id.travel_detail_logo);
		travel_detail_ticket_price = (TextView) findViewById(R.id.travel_detail_ticket_price);
		tv_travel_detail_address = (TextView) findViewById(R.id.tv_travel_detail_address);
		tv_travel_detail_phone = (TextView) findViewById(R.id.tv_travel_detail_phone);
		travel_detail_play_time = (TextView) findViewById(R.id.travel_detail_play_time);
		travel_detail_open_time = (TextView) findViewById(R.id.travel_detail_open_time);
		tv_scenic_detail_assess = (TextView) findViewById(R.id.tv_scenic_detail_assess);
		tv_scenic_detail_jjcontent = (TextView) findViewById(R.id.tv_scenic_detail_jjcontent);

		travel_detail_address = (RelativeLayout) findViewById(R.id.travel_detail_address);
		travel_detail_phone = (RelativeLayout) findViewById(R.id.travel_detail_phone);
		travel_detail_ll = (LinearLayout) findViewById(R.id.travel_detail_ll);
	}

	// 设置数据
	public void setData() {
		travel_detail_ticket_price.setText(travel_detail_ticket_price.getText()
				+ attractions.getPrice().trim());
		tv_travel_detail_address.setText(tv_travel_detail_address.getText()
				+ attractions.getAddress().trim()
				+ attractions.getName().trim());
		tv_travel_detail_phone.setText(tv_travel_detail_phone.getText()
				+ attractions.getTel().trim());
		travel_detail_play_time.setText(travel_detail_play_time.getText()
				+ attractions.getVisitTime().trim());
		travel_detail_open_time.setText(travel_detail_open_time.getText()
				+ attractions.getTime().trim());
		tv_scenic_detail_assess.setText(attractions.getEvaluate().trim());
		tv_scenic_detail_jjcontent.setText(attractions.getIntro().trim());
		RequestManager.loadImage(HappyGyApi.BASE_URL + attractions.getUrlPic(),
				RequestManager.getImageListener(travel_detail_logo,
						mDefaultImageDrawable, mDefaultImageDrawable));
		
		getActionBar().setTitle(attractions.getName().trim());
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
